package com.fs.system.sys_admin.service;

import com.fs.system.college_admin.domain.College;
import com.fs.system.common.ResponseData;
import com.fs.system.exception.CustomerException;
import com.fs.system.sys_admin.mapper.SysCollegeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class SysCollegeService {

    @Resource
    private SysCollegeMapper sysCollegeMapper;

    /**
     * 查询所有学院
     * @param college
     * @return
     */
    public List<College> selectAllCollegeList(College college) {
        return sysCollegeMapper.selectAll(college);
    }

    /**
     * 分页查询
     * @param college
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<College> selectPage(College college, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<College> colleges = sysCollegeMapper.selectAll(college);
        return PageInfo.of(colleges);
    }

    /**
     * 新增
     * @param college
     */
    public void addCollege(College college) {
        List<College> colleges = sysCollegeMapper.selectAll(college);
        if (colleges.size() > 0) {
            throw new CustomerException("500","学院已存在");
        }
        college.setDeleted(false);
        college.setCreateTime(LocalDateTime.now());
        sysCollegeMapper.addCollege(college);
    }

    /**
     * 修改
     * @param college
     */
    public void updateCollege(College college) {
        Long collegeId = college.getCollegeId();
        String name = college.getName();
        College dbCollege = sysCollegeMapper.selectCollegeById(collegeId);
        if(!dbCollege.getName().equals(name)){
            College dbCollege2 = sysCollegeMapper.selectAllByName(name);
            if (Objects.nonNull(dbCollege2)) {
                throw new CustomerException("500","学院已存在");
            }
        }
        college.setUpdateTime(LocalDateTime.now());
        sysCollegeMapper.updateCollege(college);
    }

    /**
     *  逻辑删除学院
     * @param collegeId
     * @param updateBy
     */
    public void deleteCollege(Long collegeId, String updateBy) {
        College college = new College();
        college.setCollegeId(collegeId);
        college.setDeleted(true);
        college.setUpdateTime(LocalDateTime.now());
        college.setUpdatedBy(updateBy);
        sysCollegeMapper.updateCollege(college);

    }

    /**
     * 批量逻辑删除学院
     * @param ids
     * @param updateBy
     */
    public void deleteBatch(List<Long> ids, String updateBy) {
        for (Long collegeId :ids) {
            this.deleteCollege(collegeId, updateBy);
        }
    }
}
